/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.ux.form;

import org.rails.extjs.Option;
import org.rails.extjs.data.Store;
import org.rails.extjs.form.TwinTriggerField;
import org.rails.extjs.javascript.JavaScript;

public class SearchField extends TwinTriggerField {

	public SearchField() {
		super();
	}

	public SearchField(Option configOptions) {
		super(configOptions);
	}

	public SearchField(String name) {
		super(name);
	}

	public SearchField(String name, String fieldLabel) {
		super(name, fieldLabel);
	}

	public SearchField(String name, String fieldLabel, Object value) {
		super(name, fieldLabel, value);
	}
	
	public void setStore(Store store){
		addOption("store",store);
	}
	
	public Store getStore(){
		if(getOption("store") instanceof Store)
			return (Store) getOption("store");
		else
			return null;
	}
	
	public void setStore(String storeId){
		addOption("store",new JavaScript("Ext.StoreMgr.lookup('{0}')",storeId));
	}
	
	public void setHasSearch(Boolean hasSearch){
		addOption("hasSearch",hasSearch);
	}
	
	public Boolean isHasSearch(){
		return getBoolean("hasSearch",false);
	}
	
	public void setParamName(String paramName){
		addOption("paramName", paramName);
	}
	
	public String getParamName(){
		return getString("paramName","query");
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.ux.form.SearchField(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
